from rest_framework.generics import CreateAPIView
from rest_framework.permissions import IsAdminUser,IsAuthenticated
from xyz.models import Article
from yc1997512.serializers.articlecreate import ArticlecreateSerializer
from rest_framework.response import Response


class CreateArticleAPIView(CreateAPIView):
    '''发布文章'''

    # permission_classes = [IsAuthenticated]
    queryset = Article
    serializer_class = ArticlecreateSerializer

    def create(self, request, *args, **kwargs):
        create_data = request.data
        user = request.user.id
        create_data['user'] = user
        title = create_data.get('title')
        content = create_data.get('content')
        labels1 = create_data.get('labels')
        channel = create_data.get('channel')
        image = create_data.get('image')
        for i in labels1:
            labels2 = i
        article = Article.objects.create(content=content, title=title,
                                         user_id=user, channel_id=channel,
                                         image=image)
        # 循环添加所有的标签
        article.labels.add(labels2)
        return Response({"message": "文章发布成功", "success": "True",'articleid':article.id})
